#ifndef __FLASH_H__
#define __FLASH_H__
#include "stm32f4xx.h"  
#include "delay.h"

#pragma pack(1)
typedef struct {
	unsigned int  ServoID;                    // 和功能码保持一致  
	unsigned int  AppUpgradeFlag;   	      // 舵机软件升级标志
	unsigned int  HardVersion[4];             // 硬件版本和app一定要保持一致
	unsigned int  SoftVerSion[4];             // 软件版本boot和app独立
	unsigned int  SciBaudRate;                // 通信波特率 目前默认115200 接pc 不能修改
	unsigned int  CanBaudRate;                // 和功能码保持一致   
	unsigned int  check;					  // 校验和
}DevBaseInfo;
#pragma pack()

// UWB信息
extern const DevBaseInfo  DevInfoInit;
extern DevBaseInfo DevInfoCur;
extern uint32_t UniqueSerialNumber;
extern const unsigned char DeviceInfo[7];

/*********************************************************************/
//                        变量定义
/*********************************************************************/
#define STM32_FLASH_SIZE     512
#define STM32_FLASH_WREN     1

#if STM32_FLASH_SIZE<256
#define FMC_SECTOR_SIZE 1024 //字节
#else 
#define FMC_SECTOR_SIZE	2048
#endif

/* Base address of the Flash sectors */
#define ADDR_FLASH_SECTOR_0     ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */
#define ADDR_FLASH_SECTOR_1     ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */
#define ADDR_FLASH_SECTOR_2     ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */
#define ADDR_FLASH_SECTOR_3     ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */
#define ADDR_FLASH_SECTOR_4     ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes */
#define ADDR_FLASH_SECTOR_5     ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */
#define ADDR_FLASH_SECTOR_6     ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */
#define ADDR_FLASH_SECTOR_7     ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */
#define ADDR_FLASH_SECTOR_8     ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */
#define ADDR_FLASH_SECTOR_9     ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */
#define ADDR_FLASH_SECTOR_10    ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
#define ADDR_FLASH_SECTOR_11    ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */

//-- 用途划分
#define FMC_FLASH_BASE       0x08000000 	 // FLASH的起始地址
#define BOOT_START_ADDRESS   0x08000000      // 定义Boot区间为12K
#define APP_START_ADDRESS    0x08008000      // APP程序起始地址
#define APP_CHECK_ADDRESS    0x08009000

#define FMC_FLASH_END        0x08080000      // FLASH的结束地址 64k
//#define DEVICE_INFO_SIZE     0x400           // 设备信息大小 1K 

#define DEVICE_INFO_ADDRESS  0x08004180      //（STM32_FLASH_END - DEVICE_INFO_SIZE）   // 设备信息起始地址
#define DEVICE_LOG_ADDRESS   0x08004100      //（STM32_FLASH_END - 2*DEVICE_INFO_SIZE） // 设备日志起始地址
#define DEVICE_TAB_ADDRESS   0x08004000      // 设备表地址

 
u16 STMFLASH_ReadHalfWord(u32 faddr);		  //读出半字  
void STMFLASH_WriteLenByte(u32 WriteAddr,u32 DataToWrite,u16 Len);	//指定地址开始写入指定长度的数据
u32 STMFLASH_ReadLenByte(u32 ReadAddr,u16 Len);				//指定地址开始读取指定长度数据
void STMFLASH_Write(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite);		//从指定地址开始写入指定长度的数据
void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead);   		//从指定地址开始读出指定长度的数据
uint32_t GetSector(uint32_t Address);

//测试写入
void Test_Write(u32 WriteAddr,u16 WriteData);								   
#endif

















